package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"strings"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
	"zhuandan/utils"
)

type UserInfoBody struct {
	Uid      int64   `json:"uid" from:"uid"`
	ID       int64   `json:"id" from:"id"`
	Token    string  `json:"token" from:"token"`
	Bankid   int64   `json:"bankid" from:"bankid"`
	Nickname string  `json:"nickname" from:"nickname"`
	Logo     string  `json:"logo" from:"logo"`
	Page     int     `json:"page" from:"page"`
	Pagesize int     `json:"pagesize" from:"pagesize"`
	Status   int64   `gorm:"column:status" json:"status"`
	Price    float64 `gorm:"column:price" json:"price"`
	Phone    string  `gorm:"column:phone" json:"phone"` //  手机号
	Cid      string  `gorm:"column:cid" json:"cid"`
}

func UserInfoywd(ctx *gin.Context) {
	request := UserInfoBody{}
	ctx.ShouldBind(&request)
	phone := request.Phone
	var user_info models.YwWeixin
	data_source.OtherDb.Where("mobile = ?", phone).First(&user_info)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    user_info,
		"msg":     "ok",
	})
	return
}
func Usermoneylist(ctx *gin.Context) {
	request := UserInfoBody{}
	ctx.ShouldBind(&request)
	var user_info []models.YwAccountMoney
	data_source.OtherDb.Order("id desc").Limit(500).Find(&user_info)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    user_info,
		"msg":     "ok",
	})
	return
}

func Usermoney(ctx *gin.Context) {
	request := UserInfoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var user_info []models.YwAccountMoney
	data_source.OtherDb.Where("account_id = ?", id).Order("id desc").Find(&user_info)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    user_info,
		"msg":     "ok",
	})
	return
}
func UserInfo(ctx *gin.Context) {
	request := UserInfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	var user_info models.User
	data_source.Db.Where("id = ?", uid).First(&user_info)
	if user_info.ID == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "账号不存在",
		})
		return
	}
	//var tuisongshezhi []models.Tuisongshezhi
	//data_source.Db.Where("uid = ?", uid).Where("state = 1").Find(&tuisongshezhi)
	if len(user_info.Classifyids) == 0 && len(user_info.Provincecodes) == 0 {
		user_info.Tuisong = 1
	} else {
		user_info.Tuisong = 2
	}

	//if user_info.Ywdgzheopenid != "" {
	//	var weixin models.YwWeixin
	//	data_source.OtherDb.Where("unionid = ?", user_info.Unionid).First(&weixin)
	//	if weixin.Money != 0 {
	//		//price := utils.StrToFloat64(data.User.Money)
	//		data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("price", weixin.Money)
	//		//user_info.Price = price
	//	}
	//}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    user_info,
		"msg":     "ok",
	})
	return
}

func Usershezhiinfo(ctx *gin.Context) {
	request := UserInfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	var user_info models.Usershezhi
	data_source.Db.Model(&models.User{}).Where("id = ?", uid).First(&user_info)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    user_info,
		"msg":     "ok",
	})
	return
}
func Userunionid(ctx *gin.Context) {
	unionid := ctx.Param("unionid")
	var user_info models.User
	data_source.Db.Where("unionid = ?", unionid).First(&user_info)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    user_info,
		"msg":     "ok",
	})
	return
}

func Usertx(ctx *gin.Context) {
	request := UserInfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	price := request.Price
	status := request.Status
	bankid := request.Bankid
	var user_info models.User
	data_source.Db.Where("id = ?", uid).First(&user_info)
	if user_info.Guanlian == 1 {
		if status != 3 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "只能提现到银行卡",
			})
			return
		}

	}
	if user_info.Yue < price {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "可用金额不足",
		})
		return
	}

	if price < 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "提现金额不能小于0",
		})
		return
	}
	var tixianid int64
	if status == 1 {
		var sxf float64

		if user_info.Yesxf > 0 {
			sxf = user_info.Yesxf
		} else {
			var shouxufei models.Shouxufei
			data_source.Db.Where("id = 1").First(&shouxufei)
			sxf = shouxufei.Sxf
		}
		price1 := price * (100 - sxf) / 100
		data := Appaccountuser(user_info.Unionid)
		//fmt.Println(data)
		userprice := utils.StrToFloat64(data.User.Money) + price1
		Appaccountmoney(user_info.Unionid, "用户提现到余额", "1", price1)
		data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("price", userprice)
		data_source.Db.Model(&models.User{}).Where("id = ?", uid).UpdateColumn("yue", gorm.Expr("yue - ?", price))
		//userup := models.User{
		//	Price: userprice,
		//	Yue:   user_info.Yue - price,
		//}
		//data_source.Db.Where("id = ?", uid).Updates(&userup)

		uprice := models.Userprice{
			Uid:     uid,
			Price:   price,
			State:   2,
			Addtime: time.Now().Unix(),
			Title:   "提现到余额",
		}
		data_source.Db.Create(&uprice)

		tx := models.Usertixian{
			Uid:       uid,
			Price:     price,
			State:     1,
			Addtime:   time.Now().Unix(),
			Title:     "提现到余额",
			Yuanprice: user_info.Yue,
			Sxf:       price * sxf / 100,
			Dzprice:   price1,
			Status:    1,
			Fanhui:    1,
		}
		data_source.Db.Create(&tx)
		tixianid = tx.ID
	} else if status == 2 {

		var sxf float64

		if user_info.Tgsxf > 0 {
			sxf = user_info.Tgsxf
		} else {
			var shouxufei models.Shouxufei
			data_source.Db.Where("id = 2").First(&shouxufei)
			sxf = shouxufei.Sxf
		}

		price1 := price * (100 - sxf) / 100

		data_source.Db.Model(&models.User{}).Where("id = ?", uid).UpdateColumn("yue", gorm.Expr("yue - ?", price))
		data_source.Db.Model(&models.User{}).Where("id = ?", uid).UpdateColumn("tgprice", gorm.Expr("tgprice + ?", price1))
		uprice := models.Userprice{
			Uid:     uid,
			Price:   price,
			State:   2,
			Addtime: time.Now().Unix(),
			Title:   "提现到推广账号",
		}
		data_source.Db.Create(&uprice)

		tx := models.Usertixian{
			Uid:       uid,
			Price:     price,
			State:     2,
			Addtime:   time.Now().Unix(),
			Title:     "提现到推广账号",
			Yuanprice: user_info.Yue,
			Sxf:       price * sxf / 100,
			Dzprice:   price1,
			Status:    1,
			Fanhui:    1,
		}
		data_source.Db.Create(&tx)
		tixianid = tx.ID
	} else if status == 3 {

		var sxf float64

		if user_info.Yhksxf > 0 {
			sxf = user_info.Yhksxf
		} else {
			var shouxufei models.Shouxufei
			data_source.Db.Where("id = 3").First(&shouxufei)
			sxf = shouxufei.Sxf
		}

		price1 := price * (100 - sxf) / 100

		data_source.Db.Model(&models.User{}).Where("id = ?", uid).UpdateColumn("yue", gorm.Expr("yue - ?", price))
		data_source.Db.Model(&models.User{}).Where("id = ?", uid).UpdateColumn("yhkprice", gorm.Expr("yhkprice + ?", price1))
		uprice := models.Userprice{
			Uid:     uid,
			Price:   price,
			State:   2,
			Addtime: time.Now().Unix(),
			Title:   "提现到银行卡",
		}
		data_source.Db.Create(&uprice)

		tx := models.Usertixian{
			Uid:       uid,
			Price:     price,
			State:     3,
			Addtime:   time.Now().Unix(),
			Title:     "提现到银行卡",
			Yuanprice: user_info.Yue,
			Sxf:       price * sxf / 100,
			Dzprice:   price1,
			Bankid:    bankid,
			Status:    1,
			Fanhui:    1,
		}
		data_source.Db.Create(&tx)
		tixianid = tx.ID
	}
	var tcprice float64
	if user_info.Pid > 0 {
		var tuiguang models.Tuiguang
		data_source.Db.Where("id = 1").First(&tuiguang)
		tcprice = price * float64(tuiguang.Bfb) / 100
		var puser models.User
		data_source.Db.Where("id = ?", user_info.Pid).First(&puser)
		userpid := models.User{
			Tcprice: puser.Tcprice + tcprice,
			Yue:     puser.Yue + tcprice,
		}
		data_source.Db.Where("id = ?", puser.ID).Updates(&userpid)
		uprice := models.Userprice{
			Uid:     puser.ID,
			Price:   tcprice,
			State:   1,
			Addtime: time.Now().Unix(),
			Title:   "下级用户提现收到提成",
		}
		data_source.Db.Create(&uprice)

		//r := Appaccountmoney(puser.Unionid, "下级用户提现收到提成", "1", tcprice)
		//if r.Status == 1 {
		//	data_source.Db.Model(&models.Usertixian{}).Where("id = ?", tixianid).Update("fanhui", 2)
		//}
	} else {
		data_source.Db.Model(&models.Usertixian{}).Where("id = ?", tixianid).Update("fanhui", 2)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type UserbankBody struct {
	ID        int64  `json:"id" gorm:"id"`
	Uid       int64  `json:"uid" gorm:"uid"`
	Name      string `json:"name" gorm:"name"`
	Phone     string `json:"phone" gorm:"phone"`
	Yinhang   string `json:"yinhang" gorm:"yinhang"`
	Kaihuhang string `json:"kaihuhang" gorm:"kaihuhang"`
	Bank      string `json:"bank" gorm:"bank"`
}

func Userbankdel(ctx *gin.Context) {
	request := UserbankBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Bank{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Userbankadd(ctx *gin.Context) {
	request := UserbankBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	name := request.Name
	phone := request.Phone
	yinhang := request.Yinhang
	kaihuhang := request.Kaihuhang
	bank := request.Bank
	bankadd := models.Bank{
		Uid:       uid,
		Name:      name,
		Phone:     phone,
		Yinhang:   yinhang,
		Kaihuhang: kaihuhang,
		Bank:      bank,
	}
	data_source.Db.Create(&bankadd)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Userbank(ctx *gin.Context) {
	request := UserbankBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	var bank []models.Bank
	data_source.Db.Where("uid = ?", uid).Find(&bank)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    bank,
		"msg":     "ok",
	})
	return
}

func Userupdate(ctx *gin.Context) {
	request := UserInfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	id := request.ID
	nickname := request.Nickname
	logo := request.Logo
	status := request.Status

	if id > 0 {
		data_source.Db.Model(&models.User{}).Where("id = ?", id).Update("status", status)
	} else {
		user_info := models.User{
			Logo:     logo,
			Nickname: nickname,
		}
		data_source.Db.Where("id = ?", uid).Updates(&user_info)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Userupdatecid(ctx *gin.Context) {
	request := UserInfoBody{}
	ctx.ShouldBind(&request)
	cid := request.Cid
	uid := request.Uid
	if cid == "" {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "cid不能未空",
		})
		return
	}
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "uid不能未空",
		})
		return
	}
	user_info := models.User{
		Cid: cid,
	}
	data_source.Db.Where("id = ?", uid).Updates(&user_info)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Userphone(ctx *gin.Context) {
	request := UserInfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	phone := request.Phone

	data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("phone", phone)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type Infouser struct {
	Data       []models.User `json:"data"`
	PageNo     int           `json:"pageNo"`
	PageSize   int           `json:"pageSize"`
	TotalCount int64         `json:"totalCount"`
	TotalPage  int64         `json:"totalPage"`
}

type UserlistBody struct {
	Page       int    `json:"pageNo" from:"pageNo"`
	Pagesize   int    `json:"pageSize" from:"pageSize"`
	Phone      string `json:"phone" from:"phone"`
	Vip        int64  `json:"vip" from:"vip"`
	Status     int64  `gorm:"column:status" json:"status"`
	Modelphone int64  `gorm:"column:modelphone" json:"modelphone"`

	CreateTime int64 `gorm:"column:create_time" json:"create_time"`
	LoginNum   int64 `gorm:"column:login_num" json:"login_num"`
}

func Userlist(ctx *gin.Context) {
	request := UserlistBody{}
	ctx.ShouldBind(&request)
	phone := request.Phone
	create_time := request.CreateTime
	login_num := request.LoginNum
	vip := request.Vip
	status := request.Status
	modelphone := request.Modelphone
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.User
	res := data_source.Db
	if phone != "" {
		res = res.Where("phone = ?", phone)
	}
	if vip > 0 {
		res = res.Where("vip = ?", vip)
	}
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	if modelphone > 0 {
		res = res.Where("modelphone = ?", modelphone)
	}
	if login_num > 0 {
		endlogintime := login_num + 86400
		res = res.Where("login_num > ? and login_num < ?", login_num, endlogintime)
	}
	if create_time > 0 {
		endcreatetime := create_time + 86400
		res = res.Where("create_time > ? and create_time < ?", create_time, endcreatetime)
	}
	res.Model(&models.User{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := Infouser{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type UserpriceRBody struct {
	Data       []models.Userprice `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

type UserpriceBody struct {
	Uid      int64 `json:"uid" from:"uid"`
	State    int64 `json:"state" from:"state"`
	Page     int   `json:"pageNo" from:"pageNo"`
	Pagesize int   `json:"pageSize" from:"pageSize"`
}

func Userprice(ctx *gin.Context) {
	request := UserpriceBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	state := request.State
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Userprice
	res := data_source.Db.Where("uid = ?", uid)
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Userprice{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UserpriceRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type UserxiaofeiRBody struct {
	Data       []models.Userpricenew `json:"data"`
	PageNo     int                   `json:"pageNo"`
	PageSize   int                   `json:"pageSize"`
	TotalCount int64                 `json:"totalCount"`
	TotalPage  int64                 `json:"totalPage"`
}

func Userxiaofei(ctx *gin.Context) {
	request := UserpriceBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid

	state := request.State
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)

	var count int64
	var users []models.Userpricenew
	res := data_source.Db.Where("uid = ?", uid)
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Userpricenew{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("addtime desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UserxiaofeiRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
func Tuiguanginfo(ctx *gin.Context) {

	var users models.Tuiguang
	data_source.Db.Where("id = 1").First(&users)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

type UsertgRBody struct {
	Price   float64 `json:"price"`
	Num     int64   `json:"num"`
	Content string  `json:"content"`
}

func Usertg(ctx *gin.Context) {
	request := UserpriceBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	//fmt.Println(user)

	var count int64
	data_source.Db.Model(&models.User{}).Where("pid = ?", uid).Count(&count)
	var tg models.Tuiguang
	data_source.Db.Where("id = 1").First(&tg)
	data := UsertgRBody{
		Price:   user.Tcprice,
		Num:     count,
		Content: tg.Content,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type TgusersRBody struct {
	Data       []models.User `json:"data"`
	PageNo     int           `json:"pageNo"`
	PageSize   int           `json:"pageSize"`
	TotalCount int64         `json:"totalCount"`
	TotalPage  int64         `json:"totalPage"`
}

type TgusersBody struct {
	Page     int   `json:"pageNo" from:"pageNo"`
	Pagesize int   `json:"pageSize" from:"pageSize"`
	Uid      int64 `json:"uid" from:"uid"`
}

func Tgusers(ctx *gin.Context) {
	request := TgusersBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.User
	res := data_source.Db.Where("pid = ?", uid)

	res.Model(&models.User{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := TgusersRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type UsertcRBody struct {
	Data       []models.Userprice `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

func Usertc(ctx *gin.Context) {
	request := TgusersBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)

	var count int64
	var orders []models.Userprice
	res := data_source.Db.Where("uid = ?", uid).Where("title = ?", "下级用户提现收到提成")

	res.Model(&models.Userprice{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&orders)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UsertcRBody{
		Data:       orders,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type UsertsBody struct {
	Uid           int64  `json:"uid" from:"uid"`
	Provincecodes string `gorm:"column:provincecodes" json:"provincecodes"`
	Classifyids   string `gorm:"column:classifyids" json:"classifyids"`
	Accept        int64  `json:"accept" gorm:"accept"`
	Zhuying       string `json:"zhuying" gorm:"zhuying"`
}

func Usertsaccept(ctx *gin.Context) {
	request := UsertsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	accept := request.Accept
	user := models.User{
		Accept: accept,
	}
	data_source.Db.Where("id = ?", uid).Updates(&user)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Userts(ctx *gin.Context) {
	request := UsertsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	provincecodes := request.Provincecodes
	classifyids := request.Classifyids
	zhuying := request.Zhuying
	user := models.User{
		Provincecodes: provincecodes,
		Classifyids:   classifyids,
		Zhuying:       zhuying,
	}
	data_source.Db.Where("id = ?", uid).Updates(&user)

	provinceidarr := strings.Split(provincecodes, ",")
	classifyidarr := strings.Split(classifyids, ",")

	data_source.Db.Where("uid = ?", uid).Where("state = 1").Delete(&models.Tuisongshezhi{})
	var tuisongshezhis []models.Tuisongshezhi
	for _, p := range provinceidarr {
		for _, c := range classifyidarr {
			tuisongshezhi := models.Tuisongshezhi{
				Uid:        uid,
				Provinceid: utils.StrToInt64(p),
				Classifyid: utils.StrToInt64(c),
				State:      1,
			}
			tuisongshezhis = append(tuisongshezhis, tuisongshezhi)
		}
	}
	data_source.Db.Create(&tuisongshezhis)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Usergetui(ctx *gin.Context) {
	request := UsertsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	var cids []string
	cids = append(cids, user.Cid)
	//service.Getuixx(cids)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type UsernumRBody struct {
	Bjnum int64 `json:"bjnum" from:"bjnum"`
	Qtnum int64 `json:"qtnum" from:"qtnum"`
	Thnum int64 `json:"thnum" from:"thnum"`
}

func Usernum(ctx *gin.Context) {
	request := UsertsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	var procureorders []models.Procureorder
	data_source.Db.Where("uid = ?", uid).Debug().Find(&procureorders)
	fmt.Println(procureorders)
	//var user models.User
	//data_source.Db.Where("id = ?", uid).First(&user)
	//var weixin models.YwWeixin
	//data_source.OtherDb.Where("unionid = ?", user.Unionid).First(&weixin)
	//var infobuy []models.YwWeixinInfoBuy
	//data_source.OtherDb.Where("account_id = ?", weixin.ID).Find(&infobuy)
	//fmt.Println(infobuy)
	//return
	//procureorderlist := procureorders
	//for _, p := range infobuy {
	//	cun := 1
	//	for _, v := range procureorders {
	//		if v.Addtime == p.Ctime {
	//			cun = 2
	//		}
	//	}
	//	if cun == 1 {
	//		procureorderinfo := models.Procureorder{
	//			Uid:   user.ID,
	//			State: int64(p.Progress),
	//		}
	//		procureorderlist = append(procureorderlist, procureorderinfo)
	//	}
	//
	//}

	var bjnum int64
	var qtnum int64
	var thnum int64
	for _, v := range procureorders {
		if v.State == 1 {
			bjnum = bjnum + 1
		} else if v.State == 2 {
			qtnum = qtnum + 1
		} else if v.State == 3 || v.State == 4 {
			thnum = thnum + 1
		}
	}

	data := UsernumRBody{
		Bjnum: bjnum,
		Qtnum: qtnum,
		Thnum: thnum,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type UserxjlistRBody struct {
	Data       []models.User `json:"data"`
	PageNo     int           `json:"pageNo"`
	PageSize   int           `json:"pageSize"`
	TotalCount int64         `json:"totalCount"`
	TotalPage  int64         `json:"totalPage"`
}

type UserxjlistBody struct {
	Page     int   `json:"pageNo" from:"pageNo"`
	Pagesize int   `json:"pageSize" from:"pageSize"`
	Uid      int64 `json:"uid" from:"uid"`
}

func Userxjlist(ctx *gin.Context) {
	request := UserxjlistBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.User
	res := data_source.Db.Where("pid = ?", uid)

	res.Model(&models.User{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := Infouser{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
